package recursion

type Fac struct {
	val map[int]int
}

// 阶乘问题 走楼梯
func NewFactorial() *Fac {
	return &Fac{val: map[int]int{}}
}

func (fac *Fac) Factorial(n int) int {
	if fac.val[n] != 0 {
		return fac.val[n]
	}
	if n <= 1 {
		fac.val[n] = 1
		return 1
	} else {
		res := n * fac.Factorial(n-1)
		fac.val[n] = res
		return res
	}
}

func (fac *Fac) Print(n int) int {
	return fac.val[n]
}
